var mongoose = require('mongoose');
var md5 = require('md5');
var mongodbURL = 'mongodb://127.0.0.1:27017/jbb';
var mongodbOptions = { };

mongoose.connect(mongodbURL, mongodbOptions, function (err, db) {
    if (err) { 
        console.log('Connection refused to ' + mongodbURL);
        console.log(err);
    } else {
        console.log('Connection successful to: ' + mongodbURL);
    }
});

var Schema = mongoose.Schema;

// User schema
var User = new Schema({
    username: { type: String, required: true, unique: true },    // 手机号
    password: { type: String, required: true },                  // 密码 md5
    header:   { type: String, default: ""    },                  // 头像
    type:     { type: String, default: "0"   },                  // 类型
    property: { type: Array                  },                  // 钱包资产
    asset:    { type: Number, default: 0     },                  // APP资产
    realinfo: { type: Object                 },                  // 实名信息
    otherinfo:{ type: Object                 },                  // 其他信息
    tradepwd: { type: String, default: ""    },                  // 交易密码
    nick:     { type: String, default: ""    },                  // 昵称
    status:   { type: String, default: "1"   },
    updated:  { type: Date, default: Date.now},
    created:  { type: Date, default: Date.now}
});

// 币种钱包
var Wallet = new Schema({
    userid:   { type: String, required: true },        // 用户ID
    username: { type: String, required: true },        // 手机号
    btype:    { type: String, required: true },        // 钱包类型
    asset:    { type: Number, default: 0     },        // 钱包金额
    vasset:   { type: Number, default: 0     },        // 虚拟钱包金额
    status:   { type: String, default: "1"   },
    updated:  { type: Date, default: Date.now},
    created:  { type: Date, default: Date.now}

})

// 币种种类
var Bcoin = new Schema({
    btype:    { type: String, required: true, unique: true}, // 钱包类型ID
    name:     { type: String, default: 0     },              // 钱包名称
    status:   { type: String, default: "1"   },
    created:  { type: Date, default: Date.now}
})



// Useract schema 用户活动【三种开奖活动， 自选号码必中，唯一号码必中， 奖金累计号码；活动类型：时间截止，人数截止】
// 终极大奖，通福利彩票（平台每个月累计收入10%免费投入奖金池，每期未中奖自动滚入下一期（40%， 投入10%），最终中奖收取20%手续费）
var Order = new Schema({
    userid:   { type: String, required: true },                  // 用户ID
    actid:    { type: String, required: true },                  // 活动ID
    payAmount:{ type: Number, default: 0     },                  // 投入金额
    award:    { type: Number, default: 0     },                  // 奖励金额[扣除手续费]
    proof:    { type: String, required: true },                  // 中奖凭证号，活动号+参与号
    type:     { type: String, default: "0"   },                  // 类型
    info:     { type: Object                 },                  // 其他信息
    status:   { type: String, default: "1"   },                  // 1待确认，2已参与，3未中奖，4中奖
    updated:  { type: Date, default: Date.now},
    created:  { type: Date, default: Date.now}
});


//  对账单
var Balance = new Schema({
    orderid:  { type: String, required: true, unique: true},     // 订单id
    userid:   { type: String, required: true },                  // 用户ID
    username: { type: String, required: true },                  // 用户账号
    nick:     { type: String, default: ""    },                  // 昵称
    type:     { type: String                 },                  // 0 购买 1 充值 2提现 3赎回到账
    btype:    { type: String, default: "-1"  },                  // 币种类型
    content:  { type: String, default: ""    },                  // 账单内容
    countn:   { type: Number                 },                  // 金额正负，用于累计相加计算
    pay:      { type: Number                 },                  // 金额
    tradeno:  { type: String, default: ""    },                  // 交易流水号凭证
    saddr:    { type: String, default: ""    },                  // 发送地址
    raddr:    { type: String, default: ""    },                  // 接受地址
    flag:     { type: String, default: ""    },                  // 标识
    ps:       { type: String, default: "0"   },                  // 0转入，1转出
    status:   { type: String, default: "0"   },                  // 状态1启用，0禁用
    updated:  { type: Date, default: Date.now},
    created:  { type: Date, default: Date.now}
});


//  log
var Log = new Schema({
    userid:   { type: String, required: true   },                  // 用户ID
    content:  { type: String, default: ""      },    
    detail:   { type: Object                   },   
    updated:  { type: Date,   default: Date.now},
    created:  { type: Date,   default: Date.now}
});

// https://blockchain.info/zh-cn/blocks/1504442770713 开奖说明
var Activity = new Schema({
    member:   { type: Number                 },                  // 人数
    totalm:   { type: Number, default: 0     },                  // 已参与人数
    name:     { type: String                 },                  // 活动名称
    hot:      { type: String, default: "2"   },                  // 1热门，2普通
    type:     { type: String, default: "1"   },                  // 1活动1,2活动2，3活动3
    price:    { type: Number, default: 0     },                  // 门票价格
    btype:    { type: String, default: "-1"  },                  // 0BTC 1ETH 2ETC 3LTC 4DOGE 5YBC
    remark:   { type: String                 },                  // 开奖说明
    result:   { type: Object                 },                  // 开奖结果
    img:      { type: String                 },                  // 活动图片
    resultlen:{ type: String , default: 3    },                  // 开奖号码数位
    fee:      { type: Number, default: 0.4   },                  // 手续费默认抽取
    total:    { type: Number, default: 0     },                  // 累积金额
    content:  { type: String                 },                  // 活动内容
    ok:       { type: Object                 },                  // 开奖条件
    condition:{ type: Object                 },                  // 活动条件
    actstatus:{ type: String, default: "0"   },                  // 活动状态0未开奖，1已开奖
    postlog:  { type: String, default: ""    },                  // 操作日志
    start:    { type: String                 },                  // 开始时间
    end:      { type: String                 },                  // 结束时间
    isend:    { type: String, default: "1"   },                  // 活动状态0已结束，1未结束
    status:   { type: String, default: "1"   },                  // 状态1启用，0禁用
    updated:  { type: Date, default: Date.now},
    created:  { type: Date, default: Date.now}
});




// Bcrypt middleware on UserSchema
User.pre('save', function(next) {
    var user = this;
    if (!user.isModified('password')) return next();
    user.password = md5(user.password);
    next();
});

//Password verification
User.methods.comparePassword = function(password, cb) {
    if(md5(password)==this.password){
        cb(true);
    }else{
        return cb(false);
    }
};


//Define Models
var userModel      = mongoose.model('User', User);
var activityModel  = mongoose.model('Activity', Activity);
var balanceModel   = mongoose.model('Balance', Balance);
var logModel       = mongoose.model('Log', Log);
var orderModel     = mongoose.model('Order', Order);
var walletModel    = mongoose.model('Wallet', Wallet);
var bcoinModel     = mongoose.model('Bcoin', Bcoin);



// Export Models
exports.userModel      = userModel;
exports.balanceModel   = balanceModel;
exports.activityModel  = activityModel;
exports.logModel       = logModel;
exports.orderModel     = orderModel;
exports.walletModel    = walletModel;
exports.bcoinModel     = bcoinModel;



